import numpy as np
from Model import Model
    
class MoveSegment(object):

    @staticmethod
    def calculate_speed(track_segment):
        locations = track_segment.locations
        frame_0 = locations[-2]
        frame_1 = locations[-1]
        width = track_segment.actor.width
        x_dist = np.float(frame_1[0]) - np.float(frame_0[0])
        z_dist = np.float(frame_1[1]) - np.float(frame_0[1])
        distance = np.sqrt(x_dist * x_dist + z_dist * z_dist)
        speed = distance / width * Model.FR
        return speed
    
    @staticmethod
    def identity_stat(parameters):
        return parameters
    
    @staticmethod
    def expon_predictive(chi_prev, nu, chi_new):
        chi_prev = chi_prev[0]
        chi_new = chi_new[0]
        nu_prev = nu
        nu_new = nu + 1
        return ((nu_prev + 1) * np.log(chi_prev) + np.log(nu_new) - 
                (nu_new + 1) * np.log(chi_new))
    
    @staticmethod
    def expon_joint(chi, nu, lambda_):
        """
        chi is the sum of x_i over the interval in question
        nu is the length of the interval
        lambda_ is the parameter for this particular exponential function
    
        chi and nu are already being tracked by the changepoint finder class for
        each run length (since they're the values used to fit the distribution for a
        given run length)
    
        we want to return p(data | parameters) for this set of parameters, so we're
        just returning the product of p(data_point | parameters) across all data
        points, which for an exponential distribution is the equation at the end of
        this function
        """
        lambda_ = np.float(lambda_[0])
        chi = np.float(chi[0])
        nu = np.float(nu)
        return np.log(lambda_) - lambda_ * chi / nu
